const formRules = require("../util/formRules.js");
module.exports = {
	/**
	 * 新增
	 * @url admin/banner/sys/add	前端调用的url参数地址
	 * data 请求参数 说明
	 * @param {String} title 			标题
	 * @param {String} images 			图片
	 * @param {Number} sort 			排序(越小越在前面)
	 * @param {Boolean} enable 			是否启用
	 * res 返回参数说明
	 * @param {Number} code 错误码，0表示成功
	 * @param {String} msg 详细信息
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '' };
		// 业务逻辑开始-----------------------------------------------------------
		let {
			title,
			images,
			sort = 0,
			enable = true
		} = data;

		// 参数合法校验开始-----------------------------------------------------------
		let formRulesRes = await formRules.add(event);
		if (formRulesRes.code !== 0) {
			return formRulesRes;
		}
		// 参数合法校验结束-----------------------------------------------------------

		let dbName = "lzy-banner";
		let now = new Date().getTime();
		let dataJson = {
			title,
			images,
			sort,
			enable,
			_add_user: uid,
			_add_user_name: userInfo.username,
		};

		// 执行数据库API请求
		res.id = await vk.baseDao.add({
			dbName,
			dataJson
		});
		return res;
	}
}